package com.zhb.sys.login.controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.zhb.core.utils.EncryptionUtils;
import com.zhb.sys.shiro.model.User;

@RestController
public class LoginController {
	Logger log = LoggerFactory.getLogger(LoginController.class);
	@Autowired
	RedisTemplate<Object, Object> redisTemplate;
	@Resource(name = "redisTemplate")
	ListOperations<Object, Object> listOperation;
	@Autowired
	StringRedisTemplate stringRedisTemplate;
	@Resource(name = "redisTemplate")
	ValueOperations<Object, Object> valueOperaion;
	@Resource(name = "redisTemplate")
	SetOperations<Object, Object> setOperations;
	@Resource(name = "redisTemplate")
	HashOperations<Object, Object, Object> hashOperations;
	@Resource(name = "redisTemplate")
	ZSetOperations<Object, Object> zSetOperation;
	@Autowired
	MongoTemplate mongoTemplate;
	@RequestMapping("/login")
	public ModelAndView login(User user, String checkcode, Model model, HttpServletRequest request) {
		log.info("用户开始登录");
		// 使用shiro做登录拦截
		// 判断验证码 是否正确
		// String validateCode = (String)
		// request.getSession().getAttribute("validateCode");// session中验证码
		// if (validateCode == null || !validateCode.equals(checkcode)) {
		// 验证码无效
		// model.addAttribute("msg", "验证码输入错误");
		// return "toLogin";
		// }
		// 使用shiro 进行权限控制
		Subject userSubject = SecurityUtils.getSubject();
		// 将用户名和密码 封装成 令牌
		// 方法密码 必须 传递 char[]
		UsernamePasswordToken token = new UsernamePasswordToken(user.getUserName(),
				EncryptionUtils.getEncryptionPassword(user.getPassword()).toCharArray());
		// 记住用户信息,设置记住用户信息--默认是不记住
		token.setRememberMe(true);
		try {
			// shiro 提供登陆方法
			userSubject.login(token);
			request.getSession().setAttribute("user", user.toString());
			// 跳转到主页
			return new ModelAndView("main");
		} catch (UnknownAccountException e) {
			e.printStackTrace();
			model.addAttribute("msg", "用户名不存在");
			return new ModelAndView("index");
		} catch (IncorrectCredentialsException e) {
			e.printStackTrace();
			model.addAttribute("msg", "密码错误");
			return new ModelAndView("index");
		}
	}

	/**
	 * 退出系统
	 * 
	 * @return
	 */
	@RequestMapping("/logout")
	public ModelAndView logout(HttpServletRequest request) {
		// 通知Subject 已经退出系统
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		// 销毁当前Session对象
		request.getSession().invalidate();
		return new ModelAndView("index");
	}
}
